<?php

namespace IMooc;

class Application
{
    public string $base_dir;
    protected static mixed $instance;

    public mixed $config;

    protected function __construct($base_dir)
    {
        $this->base_dir = $base_dir ?: dirname(__DIR__);
        $this->config = new Config($this->base_dir . '/config');
    }

    static function getInstance($base_dir = '')
    {
        if (empty(self::$instance)) {
            self::$instance = new self($base_dir);
        }
        return self::$instance;
    }

    function dispatch(): void
    {
        $uri = $_SERVER['SCRIPT_NAME'];
        list($c, $v) = explode('/', trim($uri, '/'));

        $c_low = strtolower($c);
        $c = ucwords($c);
        $class = '\\App\\Controller\\' . $c;
        var_dump($class, $c, $v);
        $obj = new $class($c, $v);

        $controller_config = $this->config['controller'];
        $decorators = array();
        if (isset($controller_config[$c_low]['decorator'])) {
            $conf_decorator = $controller_config[$c_low]['decorator'];
            foreach ($conf_decorator as $class) {
                $decorators[] = new $class;
            }
        }
        foreach ($decorators as $decorator) {
            $decorator->beforeRequest($obj);
        }
        $return_value = $obj->$v();
        foreach ($decorators as $decorator) {
            $decorator->afterRequest($return_value);
        }
    }
}